package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.dao.UserDao;
import net.wanho.entity.User;
import net.wanho.service.UserService;
import net.wanho.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * Author：汤小洋
 * Date：2025-06-26 11:24
 * Description：<描述>
 */
// @ResponseBody
// @Controller
@RestController
// @Component
@RequiredArgsConstructor
@Lazy
public class UserController {

    /**
     * 方式1：基于field属性注入
     */
    // @Autowired
    // @Resource
    private final UserService userService; // bean类型

    private final UserService userService2;



    /**
     * 方式2：基于setter属性注入
     */
    // @Autowired
    // public void setUserService(UserService userService) {
    //     this.userService = userService;
    // }

    /**
     * 方式3：基于构造方法注入
     */
    // @Autowired
    // public UserController(UserService userService){
    //     this.userService = userService;
    // }

    @Value("${address}")
    private String address; // 简单类型

    // 初始化方法，执行初始化操作
    @PostConstruct
    public void init(){
        address = address.toUpperCase();
    }

    // 销毁方法，执行销毁前的操作
    @PreDestroy
    public void destroy() {
        System.out.println("UserController即将销毁。。。。。。。");
    }

    @RequestMapping("/register")
    // @ResponseBody
    public String register() {
        User user  = new User(null, "tangxiaoyang", "123456", LocalDateTime.now());
        userService.register(user);
        return "注册成功";
    }

    @RequestMapping("/abc")
    // @ResponseBody
    public String abc(){
        System.out.println(userService == userService2);
        return address;
    }

    @RequestMapping("/login")
    // @ResponseBody
    public User login(){
        User user = userService.login("admin", "123");
        // System.out.println(user);
        return user;
    }

}
